package com.asia5b.a5mall.middleware.logic.viewmodel.order

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.GlobalValue
import com.asia5b.a5mall.middleware.model.order.GuoneiBean
import com.asia5b.a5mall.middleware.model.order.LogisItemBean
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.utils.WTimeUtils
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/5/25.
 */
class LogisDFVM :SimpleViewModel() {

    /**
     * 获取国内国际物流列表
     * 先根据订单号SN查询国际国内物流快递单号，再根据快递单号查询详细物流信息
     * 把查询到的所有物流详细信息保存在集合里面，然后返回这个集合
     */
    fun getExpressList2(model: OrderDetailModel) {
        baseSubscribe(orderApi.expressList2(model.sn), Consumer {
            if (it != null) {
                val guoneiBeanList = arrayListOf<GuoneiBean>()
                if (!it.guonei.isNullOrEmpty()) {
                    guoneiBeanList.addAll(it.guonei!!)
                }
                if (!it.guoji.isNullOrEmpty()) {
                    guoneiBeanList.addAll(it.guoji!!)
                }
//                providerView().getExpressList2Result(guoneiBeanList)
                getExpressList2ResultLiveData.value=guoneiBeanList
            }
        })
    }
    val getExpressList2ResultLiveData= MutableLiveData<ArrayList<GuoneiBean>>()
    val getLogisItemListResultLiveData= MutableLiveData<ArrayList<LogisItemBean>>()
    /**
     * 获取国内物流信息
     */
    fun getLogisItemListByGuonei(guoNeiExpressId: String, guoNeiExpressSn: String,model: OrderDetailModel) {
        baseSubscribe(orderApi.logisDetailListByGuonei(guoNeiExpressId, guoNeiExpressSn), Consumer {
//        baseSubscribe(orderApi.logisDetailListByGuonei("15", "4305783875297"), Consumer {

            if (!it.isNullOrEmpty()) {
                val logisDetailModel = it.get(0)

                if (GlobalValue.isDebug && false) {
                    logisDetailModel.data = arrayListOf()
                    for (index in 0..10) {
                        logisDetailModel.data!!.add(LogisItemBean().apply {
                            this.logisticsInfo = "已签收,感谢使用集运,期待\n" +
                                    "再次为您服务"
                            this.date = "2019-7-16\n" +
                                    "16:52:32"
                        })
                    }
                }

                if (logisDetailModel.data.isNullOrEmpty()) {
                    logisDetailModel.data = arrayListOf()
                    //添加默认文案
                    val date = WTimeUtils.format((model.ship_time * 1000L).toString(), WTimeUtils.text_yyyy_MM_dd_HH_mm_ss)
                    logisDetailModel.data!!.add(LogisItemBean().apply {
                        this.logisticsInfo = ResString.get(R.string.商家已配货)
                        this.date = date
                    })
                }
                if (!logisDetailModel.data.isNullOrEmpty()) {
//                    providerView().getLogisItemListResult(logisDetailModel.data!!)
                    getLogisItemListResultLiveData.value=logisDetailModel.data!!
                }
            }
        })
    }

    /**
     * 获取国际物流信息
     */
    fun getLogisItemListByGuoji(guoJiMainordercode: String,model: OrderDetailModel) {
        baseSubscribe(orderApi.logisDetailListByGuoji(guoJiMainordercode), Consumer {
            if (!it.isNullOrEmpty()) {
                val logisDetailModel = it.get(0)

                if (GlobalValue.isDebug && false) {
                    logisDetailModel.data = arrayListOf()
                    for (index in 0..10) {
                        logisDetailModel.data!!.add(LogisItemBean().apply {
                            this.logisticsInfo = "已签收,感谢使用集运,期待\n" +
                                    "再次为您服务"
                            this.date = "2019-7-16\n" +
                                    "16:52:32"
                        })
                    }
                }
                if (logisDetailModel.data.isNullOrEmpty()) {
                    logisDetailModel.data = arrayListOf()
                    //添加默认文案
                    val date = WTimeUtils.format((model.ship_time * 1000L).toString(), WTimeUtils.text_yyyy_MM_dd_HH_mm_ss)
                    logisDetailModel.data!!.add(LogisItemBean().apply {
                        this.logisticsInfo = ResString.get(R.string.商家已配货)
                        this.date = date
                    })
                }
                if (!logisDetailModel.data.isNullOrEmpty()) {
//                    providerView().getLogisItemListResult(logisDetailModel.data!!)
                    getLogisItemListResultLiveData.value=logisDetailModel.data!!
                }
            }
        })
    }
}